package com.magic.smartcity.view.more.park

import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.magic.smartcity.R
import com.magic.smartcity.beans.ParkBannerBean
import com.magic.smartcity.databinding.FragmentParkHomeBinding
import com.magic.smartcity.utils.Constant
import com.magic.smartcity.view.more.adapters.ParkBannerAdapter
import com.magic.smartcity.view.more.adapters.ParkHomeListAdapter
import com.magic.smartcity.view.more.viewmodels.ParkViewModel
import com.youth.banner.adapter.BannerAdapter

class ParkHomeFragment : Fragment() {

    private lateinit var binding: FragmentParkHomeBinding

    private val bannerList: MutableList<ParkBannerBean> = mutableListOf()
    private val bannerAdapter = ParkBannerAdapter(bannerList)
    private val parklistAdapter = ParkHomeListAdapter()

    private val viewModel by lazy {
        ViewModelProvider(this).get(ParkViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.getBannerList()
        viewModel.getParkList()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentParkHomeBinding.inflate(layoutInflater)
        setHasOptionsMenu(true)
        initView()
        return binding.root
    }

    private fun initView() {
        //初始化轮播图
        binding.parkBanner.setAdapter(bannerAdapter)
        viewModel.bannerList.observe(viewLifecycleOwner) {
            bannerAdapter.setDatas(it)
        }
        //初始化停车场列表
        binding.parkListRy.adapter = parklistAdapter
        binding.parkListRy.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
        viewModel.parkList.observe(viewLifecycleOwner) {
            parklistAdapter.setData(it)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.park_history, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.park_history) {
            Navigation.findNavController(binding.root)
            if (Constant.TOKEN==null){
                val notLogin = Snackbar.make(binding.root, "未登录", Snackbar.LENGTH_SHORT)
                notLogin.setAction("去登陆"){ v->
                    Navigation.findNavController(binding.root).navigate(R.id.loginFragment)
                }.show()
            }else{
                Navigation.findNavController(binding.root).navigate(R.id.action_parkHomeFragment_to_parkRecordFragment)
            }
        }
        return super.onOptionsItemSelected(item)
    }
}